package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hospital.common.core.domain.entity.SysDictData;
import com.hospital.common.core.domain.entity.SysUser;
import com.hospital.common.core.domain.model.LoginUser;
import com.hospital.common.helper.LoginHelper;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hospital.system.mapper.SysDictDataMapper;
import com.hospital.system.mapper.SysUserMapper;
import com.hospital.workstation.constant.DictConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.LawsRegulationsMedicineBo;
import com.hospital.workstation.domain.vo.LawsRegulationsMedicineVo;
import com.hospital.workstation.domain.LawsRegulationsMedicine;
import com.hospital.workstation.mapper.LawsRegulationsMedicineMapper;
import com.hospital.workstation.service.ILawsRegulationsMedicineService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 医疗相关法律法规Service业务层处理
 *
 * @author hospital
 * @date 2023-03-20
 */
@RequiredArgsConstructor
@Service
public class LawsRegulationsMedicineServiceImpl implements ILawsRegulationsMedicineService {

    private final LawsRegulationsMedicineMapper baseMapper;
    private final SysUserMapper sysUserMapper;
    private final SysDictDataMapper dictDataMapper;

    /**
     * 查询医疗相关法律法规
     */
    @Override
    public LawsRegulationsMedicineVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询医疗相关法律法规列表
     */
    @Override
    public TableDataInfo<LawsRegulationsMedicineVo> queryPageList(LawsRegulationsMedicineBo bo, PageQuery pageQuery) {
        // 查询法律法规类别
        List<SysDictData> sysDictDataList = dictDataMapper.selectDictDataByType(DictConstants.LAWS_CATEGORY_DICT_TYPE);
        Map<String, String> lawsCategoryMap = sysDictDataList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        // 查询法律法规
        LambdaQueryWrapper<LawsRegulationsMedicine> lqw = buildQueryWrapper(bo);
        Page<LawsRegulationsMedicineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // 设置法律法规类别标签
        result.getRecords().stream().forEach(lawsRegulationsMedicineVo ->
            lawsRegulationsMedicineVo.setLawsCategoryStr(lawsCategoryMap.get(lawsRegulationsMedicineVo.getLawsCategory())));
        return TableDataInfo.build(result);
    }

    /**
     * 查询医疗相关法律法规列表
     */
    @Override
    public List<LawsRegulationsMedicineVo> queryList(LawsRegulationsMedicineBo bo) {
        LambdaQueryWrapper<LawsRegulationsMedicine> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LawsRegulationsMedicine> buildQueryWrapper(LawsRegulationsMedicineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LawsRegulationsMedicine> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLawsRegulationsContent()), LawsRegulationsMedicine::getLawsRegulationsContent, bo.getLawsRegulationsContent());
        lqw.eq(StringUtils.isNotBlank(bo.getLawsCategory()), LawsRegulationsMedicine::getLawsCategory, bo.getLawsCategory());
        lqw.orderByDesc(LawsRegulationsMedicine::getCreateTime);
        return lqw;
    }

    /**
     * 新增医疗相关法律法规
     */
    @Override
    public Boolean insertByBo(LawsRegulationsMedicineBo bo) {
        // 获取登录用户
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (ObjectUtil.isNull(loginUser)) {
            return false;
        }
        // 新增
        LawsRegulationsMedicine add = BeanUtil.toBean(bo, LawsRegulationsMedicine.class);
        validEntityBeforeSave(add);
        SysUser sysUser = sysUserMapper.selectUserById(loginUser.getUserId());
        add.setCreateBy(sysUser.getNickName());
        add.setUpdateBy(sysUser.getNickName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改医疗相关法律法规
     */
    @Override
    public Boolean updateByBo(LawsRegulationsMedicineBo bo) {
        LawsRegulationsMedicine update = BeanUtil.toBean(bo, LawsRegulationsMedicine.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LawsRegulationsMedicine entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除医疗相关法律法规
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
